/*
 * Created by JFormDesigner on Mon May 19 01:47:34 GMT+08:00 2025
 */

package com.soft.bams.server;

import com.soft.bams.service.Bank;
import com.soft.bams.vo.RankVO;

import java.awt.*;
import java.awt.event.*;
import java.util.TreeSet;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/**
 * @author wzs
 */
public class RankPanel extends JPanel {

    Bank bank = Bank.getInstance();

    public RankPanel() {
        initComponents();
    }

    //点击按钮查看总余额
    private void totalBalance(ActionEvent e) {
        double balance = bank.totalBalance();
        moneyLabel.setText(balance+"元");
    }

    //点击按钮查看总透支额度
    private void totalCeiling(ActionEvent e) {
        double ceiling = bank.totalCeiling();
        moneyLabel.setText(ceiling+"元");
    }

    //点击按钮查看总贷款额度
    private void totalLoanAmount(ActionEvent e) {
        double loanAmount = bank.totalLoanAmount();
        moneyLabel.setText(loanAmount+"元");
    }

    //点击按钮查看排行榜
    private void rank(ActionEvent e) {
        TreeSet<RankVO> rank = bank.rank();

        //表格标题
        String[] titles = {"序号" , "身份证号" , "姓名" , "总资产"};

        //表格数据
        Object[][] data = new Object[rank.size()][titles.length];
        //序号
        int index = 0;
        for (RankVO rankVO : rank) {
            data[index] = new Object[]{
                    ++index,
                    rankVO.getPersonId(),
                    rankVO.getName(),
                    rankVO.getTotal()
            };
        }

        //表格中填充数据
        DefaultTableModel tableModel = new DefaultTableModel(data , titles);
        JTable table = new JTable(tableModel);
        //将表格添加至scrollPane中
        scrollPane.setViewportView(table);
    }

    //点击返回，可以跳转至服务界面
    private void back(ActionEvent e) {
        CardLayout card = (CardLayout)this.getParent().getLayout();
        card.show(this.getParent() , "server");

        //清空数据
        moneyLabel.setText("0.0元");
        moneyLabel.setHorizontalAlignment(SwingConstants.CENTER);
        scrollPane.setViewportView(moneyLabel);
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        totalBalanceButton = new JButton();
        totalCeilingButton = new JButton();
        totalLoanAmountButton = new JButton();
        rankButton = new JButton();
        backButton = new JButton();
        scrollPane = new JScrollPane();
        moneyLabel = new JLabel();

        //======== this ========
        setLayout(null);

        //---- totalBalanceButton ----
        totalBalanceButton.setText("\u67e5\u770b\u603b\u4f59\u989d");
        totalBalanceButton.addActionListener(e -> totalBalance(e));
        add(totalBalanceButton);
        totalBalanceButton.setBounds(35, 45, 115, 35);

        //---- totalCeilingButton ----
        totalCeilingButton.setText("\u67e5\u770b\u900f\u652f\u603b\u989d");
        totalCeilingButton.addActionListener(e -> totalCeiling(e));
        add(totalCeilingButton);
        totalCeilingButton.setBounds(35, 105, 115, 35);

        //---- totalLoanAmountButton ----
        totalLoanAmountButton.setText("\u67e5\u770b\u8d37\u6b3e\u603b\u989d");
        totalLoanAmountButton.addActionListener(e -> totalLoanAmount(e));
        add(totalLoanAmountButton);
        totalLoanAmountButton.setBounds(35, 170, 115, 35);

        //---- rankButton ----
        rankButton.setText("\u67e5\u770b\u6392\u884c\u699c");
        rankButton.addActionListener(e -> rank(e));
        add(rankButton);
        rankButton.setBounds(35, 235, 115, 35);

        //---- backButton ----
        backButton.setText("\u8fd4\u56de");
        backButton.addActionListener(e -> back(e));
        add(backButton);
        backButton.setBounds(35, 300, 115, 35);

        //======== scrollPane ========
        {

            //---- moneyLabel ----
            moneyLabel.setText("0.0\u5143");
            moneyLabel.setHorizontalAlignment(SwingConstants.CENTER);
            scrollPane.setViewportView(moneyLabel);
        }
        add(scrollPane);
        scrollPane.setBounds(200, 55, 215, 275);

        setPreferredSize(new Dimension(455, 430));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JButton totalBalanceButton;
    private JButton totalCeilingButton;
    private JButton totalLoanAmountButton;
    private JButton rankButton;
    private JButton backButton;
    private JScrollPane scrollPane;
    private JLabel moneyLabel;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
